<?php
class indexControl extends Control{
	//首页（用户未登录状态是随机文章显示）
    function index(){
		$db=K("newsview");
		$rows=$db->join("user")->order("addtime desc")->limit(8)->all();
		$j="";
		foreach($rows as $k=>$value){
			$s=explode("#",$value["keywords"]);
			$value['keyword']=$s;
			$j[]=$value;
		} 
		$rows=$j;
		$db=K("commentview");
		$reply=$db->order("retime desc")->all();
		$this->assign("rows",$rows);
		$this->assign("reply",$reply);
		$this->display();
    }
	//主页显示（博主关注用户的文章显示）
	function home(){
		if(!isset($_SESSION['uid'])){
			$this->display("login");
		}else{
			$uid=$_SESSION['uid'];
			$db=M("user");
			$user=$db->where("uid=$uid")->find();
			$follow=explode(",",$user["following"]);
			if($user["following"]){
				$user["f_num"]=count(explode(",",$user["following"]));
			}else{
				$user["f_num"]=0;
			}
			if($user["favrite"]){
				$user["l_num"]=count(explode(",",$user["favrite"]));
				$user["fav"]=explode(",",$user["favrite"]);
			}else{
				$user["l_num"]=0;
				$user["fav"]=array();
			}
			$db=K("newsview");
			if(!empty($follow[0])){
				$count = $db->join("user")->where(array("news.uid"=>$follow))->count();
				$page = new page($count,8);
				$limited=$page->limit();
				$limited=explode(",",$limited["limit"]);
				$limited=$limited[1];
				$this->assign("limited",$limited);
				$rows=$db->join("user")->where(array("news.uid"=>$follow))->order("addtime desc")->limit($page->limit())->all();
				$j="";
				foreach($rows as $k=>$value){
					$s=explode("#",$value["keywords"]);
					$value['keyword']=$s;
					$j[]=$value;
				} 
				$rows=$j;
				$t="";
				foreach($rows as $k=>$value){
					if(!empty($value["belike"])){
						$s=explode(",",$value["belike"]);
						$value["belike"]=$s;
					}
					$t[]=$value;
				} 
				$rows=$t;
				$this->assign("count",$count);
				$this->assign("page",$page->show(2));
				$this->assign("rows",$rows);
			}
			$db=K("commentview");
			$reply=$db->order("retime desc")->all();
			$this->assign("user",$user);
			$this->assign("reply",$reply);
			$this->display();
		}
	}
	function about(){
		C("debug",0);
		$this->display();
	}
	//用户个人文章显示页
	function news(){
	C("debug",0);
		if(!isset($_SESSION['uid'])){
			$this->go("index");
		}else{
			$uid=$_SESSION['uid'];
			$db=M("user");
			$user=$db->where("uid=$uid")->find();
			$follow=explode(",",$user["following"]);
			if($user["following"]){
				$user["f_num"]=count(explode(",",$user["following"]));
			}else{
				$user["f_num"]=0;
			}
			if($user["favrite"]){
				$user["l_num"]=count(explode(",",$user["favrite"]));
				$user["fav"]=explode(",",$user["favrite"]);
			}else{
				$user["l_num"]=0;
				$user["fav"]=array();
			}
			$db=K("newsview");
			if(!empty($follow[0])){
				$count = $db->join("user")->where("news.uid=$uid")->count();
				$page = new page($count,5);
				$limited=$page->limit();
				$limited=explode(",",$limited["limit"]);
				$limited=$limited[1];
				$this->assign("limited",$limited);
			$rows=$db->join("user")->where("news.uid=$uid")->order("top desc,addtime desc")->limit($page->limit())->all();
			if(!empty($rows[0]["id"])){
				$t="";
				foreach($rows as $k=>$value){
					$s=explode("#",$value["keywords"]);
					$value['keyword']=$s;
					$t[]=$value;
				} 
				$rows=$t;
				$j="";
				foreach($rows as $k=>$value){
					if(!empty($value["belike"])){
						$s=explode(",",$value["belike"]);
						$value["belike"]=$s;
					}
					$j[]=$value;
				}
				$rows=$j;
			}
				$this->assign("count",$count);
				$this->assign("page",$page->show(2));
				$this->assign("rows",$rows);
			}else{
				$this->assign("count",0);
			
			}
			$db=K("commentview");
			$reply=$db->order("retime desc")->all();
			$db=K("messageview");
			$msg=$db->where("to_uid=$uid")->field("mid,message.title,sendtime,messtype,news.id,news.title,user.uid,nickname,from_uid,photo")->all();
			if(!empty($msg)){
				$m_count=count($msg);
			}else{
				$m_count=0;
			}
			$this->assign("m_count",$m_count);
			$this->assign("user",$user);
			$this->assign("reply",$reply);
			$this->display();
		}
	}
	//添加关注
	function follow(){
		$uid=$_SESSION["uid"];
		$db=M("user");
		$rows=$db->where("uid=$uid")->find();
		$rows["following"];
		$get=$rows["following"];
		if(!empty($get)){
			$get.=",";
			$get.=$_GET["fuid"];
		}else{
			$get.=$_GET["fuid"];
		}
		$_POST["following"]=$get;
		if($db->where("uid=$uid")->save()){
			$from_uid=$_SESSION["uid"];
			$to_uid=$_GET["fuid"];
			$time=time();
			$sql=" insert into hd_message (from_uid,to_uid,title,sendtime,messtype)values('$from_uid','$to_uid','关注消息','$time','1') ";
			$db->exe($sql);
            $this->success("关注成功");
        } else {
            $this->error("关注失败");
        } 
	}
	//取消关注
	function unfollow(){
		$uid=$_SESSION["uid"];
		$db=M("user");
		$rows=$db->where("uid=$uid")->find();
		$follow=$rows["following"];
		$arr1=explode(",",$follow);
		$arr2=array($_GET["fuid"]);
		$follow = array_diff($arr1, $arr2);
		$_POST["following"]=implode(",",$follow);
		if($db->where("uid=$uid")->save()){
            $this->success("取消关注成功","",0);
        } else {
            $this->error("取消关注失败");
        } 
	}
	function my_follow(){
		if(!isset($_SESSION["uid"])){
			$this->go("login");
		}else{
			$uid=$_GET["uid"];
			$db=M("user");
			$user=$db->where("uid=$uid")->find();
			$follow=explode(",",$user["following"]);
			if($user["following"]){
				$follow=explode(",",$user["following"]);
				$user["f_num"]=count($follow);
			}else{
				$user["f_num"]=0;
			}
			if($user["favrite"]){
				$user["l_num"]=count(explode(",",$user["favrite"]));
				$user["fav"]=explode(",",$user["favrite"]);
			}else{
				$user["l_num"]=0;
			}
			$follower=$db->where($follow)->all();
			$this->assign("follower",$follower);
			$this->assign("user",$user);
			$this->display();
		}
	}
	function my_favrite(){
		if(!isset($_SESSION['uid'])){
			$this->display("login");
		}else{
			$uid=$_SESSION['uid'];
			$db=M("user");
			$user=$db->where("uid=$uid")->find();
			$follow=explode(",",$user["following"]);
			if($user["following"]){
				$user["f_num"]=count(explode(",",$user["following"]));
			}else{
				$user["f_num"]=0;
			}
			if($user["favrite"]){
				$user["l_num"]=count(explode(",",$user["favrite"]));
				$user["fav"]=explode(",",$user["favrite"]);
			}else{
				$user["l_num"]=0;
				$user["fav"]=array();
			}
			$db=K("newsview");
			if(!empty($follow[0])){
				$rows=$db->join("user")->where($user["fav"])->order("addtime desc")->all();
				$j="";
				foreach($rows as $k=>$value){
					$s=explode("#",$value["keywords"]);
					$value['keyword']=$s;
					$j[]=$value;
				} 
				$rows=$j;
				$t="";
				foreach($rows as $k=>$value){
					if(!empty($value["belike"])){
						$s=explode(",",$value["belike"]);
						$value["belike"]=$s;
					}
					$t[]=$value;
				} 
				$rows=$t;
				$this->assign("rows",$rows);
			}
			$db=K("commentview");
			$reply=$db->order("retime desc")->all();
			$this->assign("user",$user);
			$this->assign("reply",$reply);
			$this->display();
		}
	}
	function my_message(){
		if(!isset($_SESSION['uid'])){
			$this->go("index");
		}else{
			$uid=$_SESSION['uid'];
			$db=M("user");
			$user=$db->where("uid=$uid")->find();
			$follow=explode(",",$user["following"]);
			if($user["following"]){
				$user["f_num"]=count(explode(",",$user["following"]));
			}else{
				$user["f_num"]=0;
			}
			if($user["favrite"]){
				$user["l_num"]=count(explode(",",$user["favrite"]));
				$user["fav"]=explode(",",$user["favrite"]);
			}else{
				$user["l_num"]=0;
				$user["fav"]=array();
			}
			$db=K("newsview");
			if(!empty($follow[0])){
			$count = $db->join("user")->where("news.uid=$uid")->count();
				$this->assign("count",$count);
			}else{
				$this->assign("count",0);
			}
			$db=K("messageview");
			$msg=$db->where("to_uid=$uid")->field("mid,message.title,sendtime,messtype,news.id,news.title,user.uid,nickname,from_uid,photo")->order("sendtime desc")->all();
			if(!empty($msg)){
				$m_count=count($msg);
			}else{
				$m_count=0;
			}
			$this->assign("m_count",$m_count);
			$this->assign("msg",$msg);
			$this->assign("user",$user);
			$this->display();
		}
	}
	//关键字搜索
	function tag(){
		$word=isset($_POST['word'])?$_POST['word']:urldecode($_GET["words"]);
		if(!isset($_SESSION['uid'])){
			$this->display("login");
		}else{
			$uid=$_SESSION['uid'];
			$db=M("user");
			$user=$db->where("uid=$uid")->find();
			$follow=explode(",",$user["following"]);
			if($user["following"]){
				$user["f_num"]=count(explode(",",$user["following"]));
			}else{
				$user["f_num"]=0;
			}
			if($user["favrite"]){
				$user["l_num"]=count(explode(",",$user["favrite"]));
				$user["fav"]=explode(",",$user["favrite"]);
			}else{
				$user["l_num"]=0;
				$user["fav"]=array();
			}
			$db=K("newsview");
			if(!empty($follow[0])){
				$count = $db->join("user")->where("keywords like '%$word%'")->count();
				$page = new page($count,8);
				$limited=$page->limit();
				$limited=explode(",",$limited["limit"]);
				$limited=$limited[1];
				$this->assign("limited",$limited);
				$rows=$db->join("user")->where("keywords like '%$word%'")->order("addtime desc")->limit($page->limit())->all();
				$j="";
				if(!empty($rows)){foreach($rows as $k=>$value){
					$s=explode("#",$value["keywords"]);
					$value['keyword']=$s;
					$j[]=$value;
				} 
				$rows=$j;
				$t="";
				foreach($rows as $k=>$value){
					if(!empty($value["belike"])){
						$s=explode(",",$value["belike"]);
						$value["belike"]=$s;
					}
					$t[]=$value;
				} 
				$rows=$t;}
				$this->assign("count",$count);
				$this->assign("page",$page->show(2));
				$this->assign("rows",$rows);
			}
			$db=K("commentview");
			$reply=$db->order("retime desc")->all();
			$this->assign("user",$user);
			$this->assign("reply",$reply);
			$this->display();
		}
	}
	function tags(){
		$word=isset($_POST['word'])?$_POST['word']:urldecode($_GET["words"]);
		$db=K("newsview");
		$rows=$db->join("user")->where("keywords like '%$word%'")->order("addtime desc")->limit(8)->all();
		if(!empty($rows)){
			$j="";
			foreach($rows as $k=>$value){
				$s=explode("#",$value["keywords"]);
				$value['keyword']=$s;
				$j[]=$value;
			} 
			$rows=$j;
			$this->assign("rows",$rows);
		}
		$db=K("commentview");
		$reply=$db->order("retime desc")->all();
		$this->assign("reply",$reply);
		$this->display();
	}
	function search(){
		$this->display();
	}
	//用户账户信息
	function account(){
		$uid=$_SESSION["uid"];
		$db=M("category");
		$row=$db->all("catid=1");
		$db=M("user");
		$user=$db->where("uid=$uid")->find();
		$this->assign("row",$row);
		$this->assign("user",$user);
		$this->display();
	} 
	//修改用户帐号信息
	function account_save(){
		$_POST["avatar"]=isset($_POST["avatar"])? $_POST["avatar"][1][0]:$_POST["touxiang"];
		$_POST["catid"]="1";
		$db=M("category");
		if($db->save()){
            $this->success("博客设置成功");
        } else {
            $this->error("博客设置失败");
        }
	}
	//发表文章页面
	function write(){
		C("WATER_ON",1);
		$this->display();
	}
	//转载文章页面
	function reblog(){
		$id=$_GET["id"];
		$db=M("news");
		$rows=$db->where("id=$id")->find();
		$this->assign("rows",$rows);
		$this->display();
	}
	function edit(){
		$id=$_GET["id"];
		$db=M("news");
		$rows=$db->where("id=$id")->find();
		$this->assign("rows",$rows);
		$this->display();
	}
	function save(){
		$id=$_POST["id"];
		$db=M("news");
		if($db->save()){
			$uid=$_SESSION["uid"];
			$time=time();
			$sql=" update hd_user set updatetime='$time' where uid='$uid' ";
			$db->exe($sql);
            $this->go("news");
        } else {
            $this->error("文章发表失败");
        }
	}
	function del(){
		$id=$_GET['id'];
		$db=M("news");
		if($db->del($id)){
            $this->go("news");
        }else{
            $this->del("删除文章失败");
        }
	}
	function ding(){
		$id=$_GET["id"];
		$_POST["top"]=time();
		$db=M("news");
		if($db->where("id=$id")->save()){
            $this->go("news");
        } else {
            $this->go("news");
        }
	}
	//发表文章
	function add(){
		$db=M("news");
		if($db->add()){
			$uid=$_SESSION["uid"];
			$time=time();
			$sql=" update hd_user set updatetime='$time' where uid='$uid' ";
			$db->exe($sql);
            $this->go("news");
        } else {
            $this->error("文章发表失败");
        }
	}
	//收藏文章
	function favrite(){
	C("debug",0);
		$uid=$_SESSION["uid"];
		$id=$_POST['data'];
		$db=M("user");
		$user=$db->where("uid=$uid")->find();
		if(!empty($user["favrite"])){
			$user["favrite"].=",";
			$user["favrite"].=$id;
		}else{
			$user["favrite"].=$id;
		}
		$_POST["favrite"]=$user["favrite"];
		if($db->where("uid='$uid'")->save()){
			$db=M("news");
			$news=$db->where("id=$id")->find();
			$to_uid=$news["uid"];
			if(!empty($news["belike"])){
				$news["belike"].=",";
				$news["belike"].=$uid;
				$belike=$news["belike"];
			}else{
				$news["belike"].=$uid;
				$belike=$news["belike"];
			} 
			$time=time();
			$sql=" update hd_news set belike='$belike' where id='$id' "; 
			$db->exe($sql);
			$sqls=" insert into hd_message(from_uid,to_uid,title,id,sendtime,messtype) value('$uid','$to_uid','收藏文章','$id','$time','2'); ";
			$db->exe($sqls);
           echo 1;
        } else {
           echo 2;
        }
	}
	//取消文章收藏
	function unfavrite(){
		$id=$_POST['data'];
		$uid=$_SESSION["uid"];
		$db=M("user");
		$user=$db->where("uid=$uid")->find();
		$favrite=$user["favrite"];
		$arr1=explode(",",$favrite);
		$arr2=array($_POST['data']);
		$favrite = array_diff($arr1, $arr2);
		$_POST["favrite"]=implode(",",$favrite);
		if($db->where("uid=$uid")->save()){
			$db=M("news");
			$news=$db->where("id=$id")->find();
			$arr1=explode(",",$news["belike"]);
			$arr2=array($_SESSION["uid"]);
			$belike=array_diff($arr1,$arr2);
			$belike=implode(",",$belike);
			$sql=" update hd_news set belike='$belike' where id='$id' ";
			$db->exe($sql);
            echo 1;
        } else {
            echo 2;
        }
	}
	//个人空间页面
	function blog(){
	C("debug",0);
		if(isset($_SESSION['uid'])){
			$id=$_SESSION["uid"];
			$uid=isset($_GET['uid'])?$_GET['uid']:$id;
			$db=K("newsview");
			$count = $db->join("user")->where("news.uid=$uid")->count();
			$page = new page($count, 2);
			$rows=$db->join("user")->where("news.uid=$uid")->order("addtime desc")->limit($page->limit())->all();
			$db=M("user");
			$user=$db->where("uid=$id")->find(); 
			$following=$user["following"];
			$user["following"]=explode(",",$user["following"]);
			$info=$db->where("uid=$uid")->field("uid,nickname,photo,desc,username,theme")->find();
			$this->assign("user",$user);
			$this->assign("uid",$uid);
		}else{
			$uid=$_GET['uid'];
			$db=K("newsview");
			$count = $db->join("user")->where("news.uid=$uid")->count();
			$page = new page($count, 2);
			$rows=$db->join("user")->where("news.uid=$uid")->order("addtime desc")->limit($page->limit())->all();
			$db=M("user");
			$info=$db->where("uid=$uid")->field("uid,nickname,photo,desc,username,theme")->find();
		}
		$this->assign("info",$info);
		$this->assign("rows",$rows);
		$this->assign("page",$page->show(2));
		$this->display();
	}
	//个人文章显示界面
	function my_zone(){
		$nid=$_GET["id"];
		$uid=$_GET["uid"];
		$db=M("news");
		$next=$db->where("uid=$uid")->where("id<$nid")->order("id desc")->find();
		$prev=$db->where("uid=$uid")->where("id>$nid")->find();
		$next=$next["id"];
		$prev=$prev["id"];
		if(isset($_SESSION["uid"])){
			$id=$_SESSION["uid"];
			$db=M("user");
			$user=$db->where("uid=$id")->find();
			$following=$user["following"];
			$favrite=$user["favrite"];
			$user["favrite"]=explode(",",$user["favrite"]);
			$user["following"]=explode(",",$user["following"]);
			$this->assign("following",$following);
			$this->assign("user",$user);
			$this->assign("uid",$uid);
			$this->assign("nid",$nid);
		}
		$db=M("user");
		$info=$db->where("uid=$uid")->find();
		$db=K("newsview");
		$v=$db->join("user")->where("news.id=$nid")->find();
		if(!empty($v["belike"])){
			$v["hot"]=count(explode(",",$v["belike"]));
		}else{
			$v["hot"]=0;
		}
		$db=K("commentview");
		$reply=$db->where("id=$nid")->order("retime desc")->where("stat=1")->field("cid,pid,title,rebody,stat,user.uid,id,retime,good,bad,username,photo,nickname,atuid,atname")->all();
		$this->assign("v",$v);
		$this->assign("info",$info);
		$this->assign("reply",$reply);
		$this->assign("next",$next);
		$this->assign("prev",$prev);
		$this->display();
	}
	//发表评论
	function comment_post(){
	$m=$_POST['m'];
		if(isset($_POST["atuid"])){
			$id=$_POST['id'];
			$text=$_POST["rebody"];
			$text=explode(":",$text);
			$_POST["rebody"]=$text[1];
			$atname=trim($text[0]);
			$_POST["atname"]=ltrim($atname,'@');
			$_POST['retime']=time();
			$db=M("comment");
			if($db->add()){
				$sql  = " update hd_news set replycount=replycount+1 where id='$id' ";
                $db->exe($sql);
				$this->success("评论发表成功","",0);
			} else {
				$this->error("评论发表失败");
			}	
		}else{
			$id=$_POST['id'];
			$_POST["atuid"]="0";
			$_POST['retime']=time();
			$db=M("comment");
			if($db->add()){
				$sql  = " update hd_news set replycount=replycount+1 where id='$id' ";
                $db->exe($sql);
				$this->go("$m");
			} else {
				$this->error("评论发表失败");
			}	
		};
	}
	//删除评论
	function del_comment(){
		$cid=$_GET['cid'];
		$id=$_GET['id'];
		$db=M("comment");
		if($db->where("cid=$cid")->del()){
			$sql  = " update hd_news set replycount=replycount-1 where id='$id' ";
			$db->exe($sql);
			$this->success("删除评论成功！");
		}else{
			$this->error("删除配置失败！");
		}
	}
	//用户信息页面
	function user(){
		$uid=$_GET["uid"];
		$db=M("user");
		$user=$db->find("uid=$uid");
		$this->assign("user",$user);
		$this->display();
	}
	//用户信息修改页面
	function user_edit(){
		$uid=$_SESSION["uid"];
		$db=M("user");
		$rows=$db->find("uid=$uid");
		$db=M("category");
		$row=$db->all();
		$this->assign("rows",$rows);
		$this->assign("row",$row);
		$this->display();
	}
	//用户信息保存
	function user_save(){
		$_POST["photo"]=isset($_POST["photo"])? $_POST["photo"][1][0]:$_POST["touxiang"];
		$db=M("user");
		if($db->save()){
            $this->go("user");
        } else {
            $this->go("user");
        }
	}
	//@功能
	function user_at(){
		$uid=$_GET["uid"];
		$db=M("user");
		$rows=$db->find("uid=$uid");
		$db=M("category");
		$row=$db->all();
		$this->assign("rows",$rows);
		$this->assign("row",$row);
		$this->display();
	}

	//发现页面
	function discovery(){
		if(isset($_SESSION["uid"])){
			$uid=$_SESSION["uid"];
			$db=M("user");
			$rows=$db->where(array("uid"=>array("neq"=>$uid)))->order("regtime asc")->limit(24)->field("uid,nickname,photo")->all();
			$user=$db->where("uid=$uid")->find();
			$this->assign("user",$user);
		}else{
			$db=M("user");
			$rows=$db->order("regtime asc")->limit(24)->field("uid,nickname,photo")->all();
		}
		$this->assign("rows",$rows);
		$this->display();
	}
	//空间模版页面
	function theme(){
		if(!isset($_SESSION["uid"])){
			$this->go("login");
		}
		$db=M("theme");
		$theme=$db->all();
		$this->assign("theme",$theme);
		$this->display();
	}
	function theme_save(){
		if(isset($_SESSION["uid"])){
			$_POST["theme"]=$_GET["tvalue"];
			$_POST["uid"]=$_SESSION["uid"];
		}else{
			$this->go("login");
		}
		$db=M("user");
		if($db->save()){
            $this->go("blog");
        } else {
            $this->go("blog");
        }
	}
	//注册页面
	function register(){
		C("debug", 0);
		$web=__WEB__;
		$this->assign("web",$web);
		$this->display();
	}
	//接受用户注册所填信息
	function register_post(){
		$_POST['regtime']=time();
		$_POST['password']=md5($_POST['password']);
		$db=K("userrelation");
		$username = $_POST['username'];
		$x=rand(1,112);
		$_POST['nickname']=$username;
		$_POST['photo']="upload/".$x.".jpg";
		$row = $db->where("username='$username'")->select();
		if($row){
			$this->error('用户已经存在，请重新填写用户名');
		}else{
			$db->add();
			$this->go("login");
		}
	}
	//退出
	function out(){
		$_SESSION=array();
		session_destroy();
		$this->go("index");
	}
	//用户登录页面
	function login(){
		C("debug", 0);
		$web=__WEB__;
		$this->assign("web",$web);
		$this->display("index/login"); 
	}
	//验证用户登录信息
	function login_post(){
		$username=$_POST['username'];
		$pwd=md5($_POST['password']);
		$db= M("user");
		$row = $db->where("username='{$username}'")->select();
		if(!$row){
			$this->error("用户名不存在");
		}else{
		foreach($row as $v){
			$p=$v["password"];
			$uid=$v["uid"];
			$user1=$v["username"];
		}
		if(@$p==$pwd && $username==@$user1 && !$username==""){
			$_SESSION['uid']=$uid;
			$_SESSION['username'] = $username;
			$time=time();
			$sql=" update hd_user set logintime='$time' where uid='$uid' ";
			$db->exe($sql);
		$this->go("home");
		}else{
			$this->error("用户名或密码错误");
		}}
	}
}